package com.cat.controller;

import cn.hutool.crypto.digest.MD5;
import com.cat.dto.PageDTO;
import com.cat.dto.UserInfoDTO;
import com.cat.entity.UserInfo;
import com.cat.query.UserQuery;
import com.cat.result.Result;
import com.cat.service.EmployeeService;
import com.cat.service.IUserInfoService;
import com.cat.vo.UserInfoVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lmy
 * @since 2024-09-11
 */
@RestController
@RequestMapping("/user")
@Slf4j
@CrossOrigin(origins = "*")
@RequiredArgsConstructor
@Tag(name = "用户管理")
public class UserInfoController {

    private final IUserInfoService userInfoService;

    /**
     * 获取用户列表
     * @param query
     * @return
     */
    @PostMapping("/list")
    @Operation(summary = "获取用户列表")
    public Result<PageDTO<UserInfoVO>> getUserList(@RequestBody UserQuery query) {
        log.info("查询用户{}",query);
        PageDTO<UserInfoVO> userInfoVOPageDTO = userInfoService.queryUserPage(query);

        return Result.success(userInfoVOPageDTO, "请求成功");
    }

    /**
     * 修改用户状态
     * @param statusDTO
     * @return
     */
    @PostMapping("/change")
    @Operation(summary = "修改用户状态")
    public Result<String> changeStatus(@RequestBody UserInfoDTO statusDTO) {
        UserInfo userInfo = new UserInfo();
        userInfo.setId(statusDTO.getId());
        userInfo.setStatus(statusDTO.getStatus());
        userInfoService.updateById(userInfo);

        return Result.success("成功");
    }

    /**
     * 编辑修改用户
     * @param userInfoDTO
     * @return
     */
    @PostMapping("/edit")
    @Operation(summary = "修改用户")
    public Result<String> editUser(@RequestBody UserInfoDTO userInfoDTO){
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(userInfoDTO,userInfo);
        userInfoService.updateById(userInfo);

        return Result.success("成功");
    }

    /**
     * 重置用户密码
     * @param userInfoDTO
     * @return
     */
    @PostMapping("/rest_password")
    @Operation(summary = "重置用户密码")
    public Result<String> restPassword(@RequestBody UserInfoDTO userInfoDTO){
        log.info("重置用户密码:{}",userInfoDTO);
        String s = MD5.create().digestHex("123456");
//        log.info(s);

        UserInfo userInfo = new UserInfo();
        userInfo.setId(userInfoDTO.getId());
        userInfo.setPassword(s);

        userInfoService.updateById(userInfo);

        return Result.success("成功");
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @PostMapping("/delete")
    @Operation(summary = "删除用户")
    public Result<String> deleteUser(@RequestBody Map<String,List<Integer>> id){
        log.info("id:{}",id);
        List<Integer> ids = id.get("id");

        userInfoService.deleteAndAddress(ids);

        return Result.success("成功");
    }

    /**
     * 新增用户
     * @param userInfoDTO
     * @return
     */
    @PostMapping("/add")
    @Operation(summary = "新增用户")
    public Result<String> addUser(@RequestBody UserInfoDTO userInfoDTO){
        log.info("新增用户：{}",userInfoDTO);
        userInfoService.addUser(userInfoDTO);

        return Result.success("成功");
    }
}
